
layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;
    showVerifyCode()
    function showVerifyCode(){
        //浏览器缓存，需要加上随机数
        $("#verifyCode").attr("src","admin?method=getCode&t="+Math.random());
    }

    $("#verifyCode").click(function (){
        showVerifyCode();
    })

    //登录按钮
    form.on("submit(login)",function(data){
        $(this).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");
        let _this = $(this);
        let username = $("#userName").val();
        let password = $("#passWord").val();
        let code = $("#code").val();
        let role = Number( $('input[name="role"]:checked').val());
        let params = {};
        params.userName = username;
        params.passWord = password;
        params.code = code;
        params.role = role;
        $.ajax({
            url: API_BASE_URL+'/admin?method=doLogin',
            method:"POST",
            data:params,
            success(res){
                if (res.code == 200){
                    localStorage.setItem("username",username)
                    if (role === 0)
                    {
                        localStorage.setItem("role","admin")
                        window.location.href = "admin?method=toIndex";
                    }else {
                        localStorage.setItem("role","user")
                        window.location.href = "/news";
                    }
                }else {
                    layer.msg(res.message);
                    showVerifyCode()
                    _this.text("登录").removeAttr("disabled").removeClass("layui-disabled");
                }
            }
        })
        return false;
    })
    

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
